package framework.http.bean;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;

import framework.http.Http;
import framework.http.net.DealHttp;
import framework.strings.CharsetStr;
import framework.utils.LogUtils;

import android.text.TextUtils;


public class HttpParam {

	private ArrayList<String> mKeys = new ArrayList<String>();
	private ArrayList<Object> mValues = new ArrayList<Object>();
	private boolean isMultipart;

	public HttpParam() {

	}

	public HttpParam(boolean isMultipart) {
		this.isMultipart = isMultipart;
	}

	public void add(String key, String value) {
		if (!TextUtils.isEmpty(key) && !TextUtils.isEmpty(value)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, URLEncoderStr value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, int value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, long value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, boolean value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, double value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, char value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, float value) {
		if (!TextUtils.isEmpty(key)) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public void add(String key, File value) {
		if (!TextUtils.isEmpty(key) && value != null) {
			this.mKeys.add(key);
			this.mValues.add(value);
		}
	}

	public int size() {
		return mKeys.size();
	}

	public String getKey(int location) {
		if (location >= 0 && location < this.mKeys.size()) {
			return this.mKeys.get(location);
		}
		return "";
	}

	public Object getValue(String key) {
		int index = this.mKeys.indexOf(key);
		if (index >= 0 && index < this.mKeys.size()) {
			return this.mValues.get(index);
		} else {
			return null;
		}
	}

	public Object getValue(int location) {
		if (location >= 0 && location < this.mKeys.size()) {
			return this.mValues.get(location);
		}
		return null;
	}

	public String paramForGet() {
		if (mKeys == null || mKeys.size() <= 0) {
			return "";
		}

		StringBuilder sb = new StringBuilder();
		boolean first = true;

		for (int loc = 0; loc < size(); loc++) {
			if (first) {
				first = false;
			} else {
				sb.append("&");
			}

			String _key = getKey(loc);

			String _value = null;

			Object value = getValue(_key);
			if (value instanceof String) {
				_value = URLEncoder.encode((String) value);
			} else if (value instanceof URLEncoderStr) {
				_value = ((URLEncoderStr) value).getStr();
			} else {
				_value = String.valueOf(value);
			}

			LogUtils.log("key:" + _key + " value:" + _value);
			if (!TextUtils.isEmpty(_key) && !TextUtils.isEmpty(_value)) {
				sb.append(_key + "=" + _value);
			}
		}
		return sb.toString();
	}

	public boolean isMultipart() {
		return this.isMultipart;
	}
}
